package com.baoming.web.action;

import java.util.List;

import com.baoming.container.ServiceContainer;
import com.baoming.domain.Article;
import com.baoming.domain.Member;
import com.baoming.service.IArticleService;
import com.baoming.tool.MemberTool;
import com.baoming.util.PageBeanUtil;
import com.baoming.util.PageInfo;
import com.baoming.util.ServiceTool;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class ArticleAction extends BaseAction implements ModelDriven<Article>{

	private IArticleService articleService = (IArticleService) ServiceContainer.getService(IArticleService.SERVICE); 
	private Article article = new Article() ; 
	
	/**
	 * 文章修改保存
	 * @return
	 */
	public String update(){
		//得到要修改的文章的id
		String id = article.getId() ; 
		//数据库查询原来文章的信息
		Article oldArticle = articleService.selectArticle(id) ; 
		
		//设置修改的值 
		oldArticle.setContent(article.getContent()) ; //修改内容
		oldArticle.setDescribes(article.getDescribes()) ; //修改内容描述
		oldArticle.setSort(article.getSort()) ; //修改文章的排序
		oldArticle.setIsShow(article.getIsShow()) ;  //可视修改
		oldArticle.setTitle(article.getTitle()) ; //文章标题修改
		
		//保存数据库
		articleService.modifyArticle(oldArticle) ; 
		
		//设置返回提示信息
		request.getSession().setAttribute("article_tip", 2) ; 
		 
		return "update" ; 
	}
	
	/**
	 * 文章修改
	 * @return
	 */
	public String modify(){
		String id = request.getParameter("ids") ; 
		if(id != null){
			Article article = articleService.selectArticle(id) ; 
			request.setAttribute("article", article) ;
		}
		return SUCCESS ; 
	}
	
	/**
	 * 删除文章 
	 * @return
	 */
	public String delete(){
		
		String ids[] = article.getIds() ; 
		//如果用户做选择
		if(ids == null){
			return "delete" ; 
		}
		for(String id : ids){ 
			articleService.deleteArticle(id) ; 
		} 
		if(ids.length > 0 ){
			request.getSession().setAttribute("article_tip", 1) ; 
		} 
		return "delete" ; 
	}
	
	/**
	 * 文章管理
	 * @return
	 */
	public String detail(){
		
		//得到文章的ID
		String articleId = request.getParameter("id") ; 
		Article article = articleService.selectArticle(articleId) ; 
		request.setAttribute("article", article) ; 
		
		//更新文章点击量
		article.setViewCount(article.getViewCount()+1) ; 
		articleService.modifyArticle(article) ; 
	 
		return SUCCESS ; 
	}
	
	/**
	 * 信息管理
	 */
	public String manager(){
		
		String memberId = MemberTool.getGlobalMemberId(request) ;  
		PageInfo pageInfo = new PageInfo(request) ; 
		List<Article> articles = null ; 
		
		/**
		 * 添加搜索 
		 */
		String keyword = request.getParameter("keyword") ; 
		if(keyword != null && keyword.trim().length() > 0){ 
			keyword = ServiceTool.iosToUtf8(keyword) ; 
			/**
			 * 分页搜索 
			 */
			articles = articleService.selectSearchArticleWithPage(keyword.trim() , pageInfo, memberId) ; 
			request.setAttribute("keyword", keyword) ; 

		}else{
			/**
			 * 默认显示所有文章 
			 */
			articles = articleService.selectArticleWithPage(pageInfo , memberId) ; 
		}
		  
		request.setAttribute("articles", articles) ; 
		PageBeanUtil.setPage(pageInfo, request) ; 
		
		return SUCCESS ; 
	}
	
	/**
	 * 文章编辑
	 * @return
	 */
	public String edit(){
		 
		return SUCCESS ; 
	}

	@Override
	public String remove() {
		return null;
	}
 
	@Override
	public String add() {
		
		Member member = MemberTool.getGlobalMember(request) ; 
		
		article.setAddIp(request.getRemoteHost()) ;  
		article.setIsDelete(1) ; 
		article.setAddTime(ServiceTool.getNowDate()) ; 
		article.setMemberId(member.getId()) ;  		
		articleService.saveArticle(article) ; 
		
		return "add" ;
	}

	@Override
	public String removeGroup() {
		return null;
	}

	public Article getModel() {
		return article ;
	}
	
}
